/**
 * Created by LuoQuan on 2017/11/10.
 */

(function (cs, width, height) {
    var verifyCodes = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    var defaults = {
        //画布
        canvas: cs || null,
        width: width,
        height: height,
        randomText: true,
        randomColours: true,
        colour1: null,
        colour2: null,
        font: 'normal 40px "Comic Sans MS", cursive, sans-serif',

        //回调函数
        onSuccess: function () {
        },
        onFailure: function () {
        }
    };

    var CAPTCHA = function (config) {
        var that = this;

        this._settings = $.extend({}, defaults, config || {});

        this._canvas = this._settings.canvas;

        this._context = this._canvas.getContext('2d');

    };

    CAPTCHA.prototype = {
        //生成验证图片
        generate: function () {
            var context = this._context;

            if (null === this._settings.text || null === this._settings.text) {
                this._settings.randomText = true;
            }

            if (this._settings.randomText) {
                this._generateRandomText();
            }

            if (this._settings.randomColours) {
                this._settings.colour1 = this._generateRandomColour();
                this._settings.colour2 = this._generateRandomColour();
            }

            var gradient1 = context.createLinearGradient(0, 0, this._settings.width, 0);
            gradient1.addColorStop(0, this._settings.colour1);
            gradient1.addColorStop(1, this._settings.colour2);

            context.fillStyle = gradient1;
            context.fillRect(0, 0, this._settings.width, this._settings.height);

            context.fillStyle = "rgba(255,255,255,0.65)";
            context.fillRect(0, 0, this._settings.width, this._settings.height);

            var gradient2 = context.createLinearGradient(0, 0, this._settings.width, 0);
            gradient2.addColorStop(0, this._settings.colour2);
            gradient2.addColorStop(1, this._settings.colour1);

            context.font = this._settings.font;
            context.fillStyle = gradient2;

            context.setTransform((Math.random() / 10) + 0.9, //scalex
                0.1 - (Math.random() / 5), //skewx
                0.1 - (Math.random() / 5), //skewy
                (Math.random() / 10) + 0.9, //scaley
                (Math.random() * 20) + 10, //transx
                100); //transy

            context.fillText(this._settings.text, 0, 0);

            context.setTransform(1, 0, 0, 1, 0, 0);

            var numRandomCurves = Math.floor((Math.random() * 3) + 5);

            for (var i = 0; i < numRandomCurves; i++) {
                this._drawRandomCurve();
            }

            return this._settings.text;
        },

        //生成随机验证码
        _generateRandomText: function () {
            this._settings.text = '';
            var length = Math.floor((Math.random() * 3) + 6);
            for (var i = 0; i < length; i++) {
                this._settings.text += verifyCodes.charAt(Math.floor(Math.random() * verifyCodes.length));
            }
        },

        //绘制干扰线
        _drawRandomCurve: function () {

            var ctx = this._context;

            var gradient1 = ctx.createLinearGradient(0, 0, this._settings.width, 0);
            gradient1.addColorStop(0, Math.random() < 0.5 ? this._settings.colour1 : this._settings.colour2);
            gradient1.addColorStop(1, Math.random() < 0.5 ? this._settings.colour1 : this._settings.colour2);

            ctx.lineWidth = Math.floor((Math.random() * 4) + 2);
            ctx.strokeStyle = gradient1;
            ctx.beginPath();
            ctx.moveTo(Math.floor((Math.random() * this._settings.width)), Math.floor((Math.random() * this._settings.height)));
            ctx.bezierCurveTo(Math.floor((Math.random() * this._settings.width)), Math.floor((Math.random() * this._settings.height)),
                Math.floor((Math.random() * this._settings.width)), Math.floor((Math.random() * this._settings.height)),
                Math.floor((Math.random() * this._settings.width)), Math.floor((Math.random() * this._settings.height)));
            ctx.stroke();
        },

        _generateRandomColour: function () {
            return "rgb(" + Math.floor((Math.random() * 255)) + ", " + Math.floor((Math.random() * 255)) + ", " + Math.floor((Math.random() * 255)) + ")";
        }

    };

    Ext.Captcha = CAPTCHA || {};
}());
